package com.zzw.service.Impl;

import com.zzw.bean.Content;
import com.zzw.bean.ContentExample;
import com.zzw.dao.ContentMapper;
import com.zzw.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentMapper;

    @Override
    public void save(Content content) {
        contentMapper.insertSelective(content);
    }

    @Override
    public List<Content> list() {
        List<Content> contents = contentMapper.selectByExample(null);
        return contents;
    }

    @Override
    public List<Content> selectContent(Content content) {
        //return contentMapper.selectContent(content);
        return null;
    }

    @Override
    public List<Content> listByCondition(Content content) {
        String rejectReason = content.getRejectReason();
        Date createTime = content.getCreateTime();

        ContentExample contentExample = new ContentExample();
        //通过criteria对象 设置查询条件
        ContentExample.Criteria criteria = contentExample.createCriteria();

        //查询条件不能是空白字符, 长度大于0
        if (StringUtils.hasText(rejectReason)) {
            criteria.andRejectReasonLike("%" + rejectReason + "%");
        }
        if (createTime != null) {
            criteria.andCreateTimeEqualTo(createTime);
        }
        contentExample.setOrderByClause("create_time desc");
        //如果没有传值, 为 null, "", 或者有空白字符时, 依然是查询所有数据
        return contentMapper.selectByExample(contentExample);
    }

    @Override
    public void deleteById(Integer id) {
        contentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Content content) {
        contentMapper.updateByPrimaryKeySelective(content);
    }
}
